package eu.gline.km;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import eu.gline.km.activities.MultimediaKeyboard;
import eu.gline.km.clients.KmClient;
import eu.gline.km.jni.enums.bVk;
import eu.gline.km.jni.enums.dwFlags;

import android.app.Activity;
import android.content.Intent;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;

public class vkm extends Activity implements OnClickListener, OnKeyListener, TextWatcher {
    
	private EditText hostNameBox;
	//private EditText keyBox;
	private Button connectButton;
	private Button bkey;
	private Button bmo;
	private Core core;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        connectButton = (Button) findViewById(R.id.connectButton);
        //keyBox = (EditText) findViewById(R.id.keyBox);
        bkey = (Button) findViewById(R.id.buttonkey);
        bmo = (Button) findViewById(R.id.multimedia);
        hostNameBox = (EditText) findViewById(R.id.hostNameBox);
        
        
        connectButton.setOnClickListener(this);
        bkey.setOnClickListener(this);
        bmo.setOnClickListener(this);
        //keyBox.
        
        core = (Core)getApplication();
        
//        try {
//			Socket socket = new Socket("192.168.212.2", 9900);
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    }


	public void onClick(View v) {
		
		KmClient kmc = core.getClient("test");
		
		switch (v.getId()) {
		case R.id.connectButton:
			core.connectTo("test", hostNameBox.getText().toString(), 9900, null);
			break;
		case R.id.buttonkey:
			kmc.getKeyboard().foregroundWindow("Notepad");
			kmc.getKeyboard().keyboardEvent(bVk.VK_T, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_E, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_S, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_T, dwFlags.KEYEVENTF_KEYDOWN);
			break;
		case R.id.multimedia:
			Intent i = new Intent(getBaseContext(), MultimediaKeyboard.class);
			startActivity(i);
			break;
		}
		
		
		
		
		
	}
	
	private void mousePath(KmClient kmc) throws InterruptedException {
		kmc.getMouse().mousePos(600, 600);
		for (int i=0; i<100; i++) {
			kmc.getMouse().mouseRight(2);
			Thread.sleep(10);
		}
		for (int i=0; i<50; i++) {
			kmc.getMouse().mouseDown(2);
			Thread.sleep(10);
		}
		for (int i=0; i<100; i++) {
			kmc.getMouse().mouseLeft(2);
			Thread.sleep(10);
		}
		for (int i=0; i<50; i++) {
			kmc.getMouse().mouseUp(2);
			Thread.sleep(10);
		}
	}


	public boolean onKey(View v, int keyCode, KeyEvent event) {
		
		//KmClient kmc = core.getClient("test");
		//kmc.getKeyboard().keyboardEvent(bVk.VK_0, dwFlags.KEYEVENTF_KEYDOWN);
		//hostNameBox.setText("test");
		return false;
	}


	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}


	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}


	public void onTextChanged(CharSequence s, int start, int before, int count) {
		hostNameBox.setText(s);
	}
}